package com.neuedu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.dto.PrescriptionList;
import com.neuedu.entity.Prescription;
import com.neuedu.entity.Register;
import com.neuedu.mapper.PrescriptionMapper;
import com.neuedu.service.DrugInfoService;
import com.neuedu.service.PrescriptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.service.RegisterService;
import com.neuedu.vo.PrescriptionPayVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 处方表 服务实现类
 * </p>
 *
 * @author LiYang
 * @since 2024-11-19
 */
@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements PrescriptionService {

    @Resource
    RegisterService registerService;
    @Resource
    DrugInfoService drugInfoService;
    @Override
    public Boolean add1(String content) {
        // 将json字符串转为 list数组
        List<Prescription> list = JSONObject.parseArray(content, Prescription.class);
        return this.saveBatch(list); // 批量插入
    }
    @Override
    public Boolean add2(PrescriptionList data) {
        List<Prescription> list = data.getList();
        return this.saveBatch(list);
    }
    @Override
    public List<Prescription> list(String caseNumber, String drugState) {
        Register register = registerService.getByCaseNumber(caseNumber);
        List<Prescription> list = this.getByRegisterId(register.getId(), drugState);
        list.forEach(entry -> {
            entry.setDrugInfo(drugInfoService.getById(entry.getDrugId()));
        });
        return list;
    }

    @Override
    public Boolean updateState(Integer[] ids) {
        UpdateWrapper<Prescription> wrapper = new UpdateWrapper<>();
        wrapper.in("id", ids);
        Prescription prescription = new Prescription();
        prescription.setDrugState("已缴费");
        return this.update(prescription, wrapper);
    }
    @Override
    public List<Prescription> fayao(String caseNumber) {
        return this.list(caseNumber, "已缴费");
    }

    @Override
    public Boolean update(Integer id) {
        Prescription prescription = new Prescription(id, "已发药");
        return this.updateById(prescription);
    }
    private List<Prescription> getByRegisterId(Integer registerId, String drugState) {
        QueryWrapper<Prescription> wrapper = new QueryWrapper<>();
        wrapper.eq("register_id", registerId)
                .eq("drug_state", drugState);
        return this.list(wrapper);
    }

    @Override
    public PrescriptionPayVo selectPay(String caseNumber, String checkState) {
        Register register = registerService.getByCaseNumber(caseNumber);
        List<Prescription> list = this.lambdaQuery()
                .eq(Prescription::getRegisterId, register.getId())
                .eq(Prescription::getDrugState, checkState)
                .list();
        for (Prescription prescription : list) {
            prescription.setDrugInfo(drugInfoService.getById(prescription.getDrugId()));
        }
        return new PrescriptionPayVo(register, list);
    }

    @Override
    public Boolean refund(Integer[] ids) {
        return this.lambdaUpdate()
                .in(Prescription::getId, ids)
                .update(new Prescription(null, "已退费"));
    }
}
